#!/bin/bash --
# Copyright (c) 2016, Google Inc. Please see the AUTHORS file for details.
# All rights reserved. Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

if [ $# -ne 1 ]; then
  echo "Usage: tool/set_version <version>"
  echo
  echo "Updates pubspecs to specified version."
fi

version=$1

for pubspec in */pubspec.yaml; do
  # Remove any dependency overrides, strip trailing empty lines.
  sed -i -n '/dependency_overrides:/q;p' \
      "$pubspec"
  sed -i -e :a -e '/^\n*$/{$d;N;};/\n$/ba' "$pubspec"

  # Update versions.
  sed -i -e \
      "s#^version: .*#version: $version#" \
      "$pubspec"

  # Update dependencies.
  sed -i -e \
      "s#.*built_value_generator: .*#  built_value_generator: ^$version#" \
      "$pubspec"
done
